﻿using ERPPublic;
using MediatR;
using Newtonsoft.Json;
using SqlSugar;
using WarehouseManagement.Application.Command.OutboundShipmentsCommand;
using WarehouseManagement.Domain.DTO.OutvoundRequests;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;
using static ERPPublic.ParseAPI;

namespace WarehouseManagement.Application.CommandHandler.OutboundShipmentsCommandHandlers
{
    public class OutboundShipmentsSetCommandHandlers : IRequestHandler<OutboundShipmentsSetCommand, OutboundRequestSetDto>
    {
        protected readonly IOutboundRequestRepository outbound;// 出库请求仓储接口
        protected readonly IOutboundRProductRepository outboundR; // 仓库出库申请产品关系仓储接口

        public OutboundShipmentsSetCommandHandlers(IOutboundRequestRepository outbound, IOutboundRProductRepository outboundR)
        {
            this.outbound = outbound;
            this.outboundR = outboundR;
        }

        public async Task<OutboundRequestSetDto> Handle(OutboundShipmentsSetCommand request, CancellationToken cancellationToken)
        {
            List<Manpower> people = await GetPeopleList(1, 100);
            var outband = (await outbound.GetAll()).First(x => x.OutboundRequestId == request.id);
            OutboundRequestSetDto newoutband = new OutboundRequestSetDto();
            newoutband.ManpowerId = outband.ManpowerId;
            newoutband.ManpowerName = people.First(x => x.ManpowerId == outband.ManpowerId).MName;
            newoutband.OutboundNumber = outband.OutboundNumber;
            newoutband.ORapplicationPeople = outband.ORapplicationPeople;
            newoutband.ORapplicationDate = outband.ORapplicationDate;
            newoutband.OROutType = outband.OROutType;
            newoutband.ORConsignee = outband.ORConsignee;
            newoutband.ORPhone = outband.ORPhone;
            newoutband.ProvinceId = outband.ProvinceId;
            newoutband.CityId = outband.CityId;
            newoutband.ORAddress = outband.ORAddress;
            newoutband.ORTotalAmount = outband.ORTotalAmount;
            newoutband.AddName = outband.AddName;
            newoutband.AddTime = outband.AddTime;
            newoutband.DelName = outband.DelName;
            newoutband.DelTime = outband.DelTime;
            newoutband.IsDel = outband.IsDel;
            newoutband.Office_Id = outband.Office_Id;
            newoutband.User_Id = outband.User_Id;
            newoutband.OutboundRequestId = outband.OutboundRequestId;
            newoutband.ORIsState = outband.ORIsState;

            List<Products> pro = await GetProductList("", "", 1, 100);
            List<OutboundRProduct> outrands = (await outboundR.GetAll()).Where(x => x.OutboundNumber == outband.OutboundNumber).ToList();

            newoutband.oroDto = (from a in pro
                                 join b in outrands on a.ProductId equals b.ProductId
                                 select new OrpCreateDtos
                                 {
                                     ProductId = a.ProductId,
                                     pName = a.PName,
                                     pNumber = a.PNumber,
                                     OutboundNumber = b.OutboundNumber,
                                     ORQuantityNum = b.ORQuantityNum,
                                     ORQPrice = b.ORQPrice,
                                     AddName = b.AddName,
                                     AddTime = b.AddTime,
                                     DelName = b.DelName,
                                     DelTime = b.DelTime,
                                     IsDel = b.IsDel,
                                     Office_Id = b.Office_Id,
                                     User_Id = b.User_Id,
                                     OutboundRProductId = b.OutboundRProductId,
                                     OUTnum = b.OUTnum
                                 }
                                 ).ToList();
            return newoutband;
        }

        /// <summary>
        /// 显示人员信息
        /// </summary>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetPeopleList(int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string chanpin = "http://116.205.121.197:8000/vendor/api/Manpower/GetManpowerList?Pindex=" + pindex + "&Psize=" + psize;
            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Manpower>>(postReponse);
            return json.data.list;
        }

        /// <summary>
        /// 显示信息
        /// </summary>
        public class Manpower : Audit
        {
            /// <summary>
            /// 人员档案id
            /// </summary>
            public int ManpowerId { get; set; }
            /// <summary>
            /// 档案编号
            /// </summary>
            public string? MNumber { get; set; }
            /// <summary>
            /// 登录账号
            /// </summary>
            public string? Users { get; set; }
            /// <summary>
            /// 真实姓名
            /// </summary>
            public string? MName { get; set; }
            /// <summary>
            /// 性别
            /// </summary>
            public bool? MSex { get; set; }
            /// <summary>
            /// 出生日期
            /// </summary>
            public DateTime? MBirthDay { get; set; }
            /// <summary>
            /// 身份证号码
            /// </summary>
            public string? MIdCard { get; set; }
            /// <summary>
            /// 联系电话
            /// </summary>
            public string? MPhone { get; set; }
            /// <summary>
            /// 电子邮箱
            /// </summary>
            public string? MEmail { get; set; }
            /// <summary>
            /// 微信号
            /// </summary>
            public string? MWeChat { get; set; }
            /// <summary>
            /// QQ号
            /// </summary>
            public string? MQQ { get; set; }
            /// <summary>
            /// 紧急联系人
            /// </summary>
            public string? MContact { get; set; }
            /// <summary>
            /// 紧急联系人电话
            /// </summary>
            public string? MCPhone { get; set; }
            /// <summary>
            /// 户口地址
            /// </summary>
            public string? MPermanent { get; set; }
            /// <summary>
            /// 家庭住址
            /// </summary>
            public string? MResidence { get; set; }
            /// <summary>
            /// 证件照
            /// </summary>
            public string? MIdPhoto { get; set; }
            /// <summary>
            /// 入职时间
            /// </summary>
            public DateTime? MEntryDate { get; set; }
            /// <summary>
            /// 所在部门
            /// </summary>
            public int? SectorId { get; set; }
            /// <summary>
            /// 所在岗位
            /// </summary>
            public int? PostId { get; set; }

        }
        /// <summary>
        /// 显示产品信息
        /// </summary>
        /// <param name="PName"></param>
        /// <param name="PNumber"></param>
        /// <param name="pindex"></param>
        /// <param name="psize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetProductList(string? PName, string? PNumber, int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI1MCIsInNpZCI6IjE4NjcwOTU3MzQ0NjIwNTg0OTYiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMTksMTkiLCJqdGkiOiI1MGYyMWRmZS0yNWQ3LTQ1OTQtOTg1Yi0wZTM2MDhhZjJiNmYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoi5LuY5rSb5ZiJIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvdXJpIjoiLyIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0._wh23n1M7asMGST9NyeZj-5rk5ICAiOFv6yDk6e2o7Y";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://116.205.121.197:8000/erpcp/api/Product/ProductShow?PName=" + PName + "&PNumber=" + PNumber + "&ProductTypeId=" + 0 + "&PageIndex=" + pindex + "&PageSize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }


        //产品
        public class Products
        {
            /// <summary>
            /// 产品Id
            /// </summary>
            [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
            public int ProductId { get; set; }
            /// <summary>
            /// 标识
            /// </summary>

            public int? PCharacteristic { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PNumber { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PName { get; set; }
            /// <summary>
            /// 规格型号
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PSpec { get; set; }
            /// <summary>
            /// 单位
            /// </summary>
            /// 
            [SugarColumn(ColumnDataType = "Nvarchar(50)")]//设计长度
            public string? PUnit { get; set; }

            /// <summary>
            /// 产品分类ID
            /// </summary>

            public int ProductTypeId { get; set; }




        }
    }
}
